package service

import (
	"github.com/jinzhu/gorm"
	"github.com/xdhuxc/asset-management-system/src/model"
)

type userService struct {
	db *gorm.DB
}

func newUserService(db *gorm.DB) *userService {
	return &userService{db}
}

func (us *userService) Auth(name string, password string) (*model.User, error) {
	var user model.User

	err := us.db.Model(&model.User{}).
		Select([]string{"id", "name", "description"}).
		Where("name = ? AND password = ?", name, password).First(&user).Error

	if err != nil && err == gorm.ErrRecordNotFound {
		return nil, err
	}

	return &user, nil
}
